package vip.xiaonuo.modular.reactor;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.reactor.dto.MemberAttendanceVo;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.modular.attendanceshiftgroup.service.AttendanceShiftGroupService;

import javax.annotation.Resource;

import static reactor.event.selector.Selectors.$;

@Service
public class MemberAttendanceListener implements Consumer<Event<MemberAttendanceVo>>, InitializingBean {

    @Resource
    private AttendanceShiftGroupService attendanceShiftGroupService;


    @Autowired
    private Reactor reactor;

    @Override
    public void accept(Event<MemberAttendanceVo> event) {
        MemberAttendanceVo data = event.getData();
        attendanceShiftGroupService.memberAttendance(data.getMemberId(), data.getTime());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.MEMBER_ATTENDANCE), this);
    }
}
